package com.ziq.base.transition

import android.os.Parcel
import android.os.Parcelable
import android.view.View
import android.widget.TextView
import androidx.annotation.CallSuper
import com.ziq.base.R
import com.ziq.base.transition.custom.ChangeTextTransition

class ShareElementInfo<T : ShareElementInfoDataUpdate> {

    var mView: View

    var clz : Class<T>? = null

    constructor(transitionName:String, view: View, clz: Class<T>? = null) {
        this.mView = view
        this.clz = clz
        mView.transitionName = transitionName
    }

    fun getView(): View{
        return mView
    }

    class ShareElementInfoData<T : ShareElementInfoDataUpdate>() : Parcelable {
        var status:String? = ""
        var snapShot: Parcelable? = null
        var clz : Class<T>? = null
        private var customTempData: T? = null
        var customFromData: T? = null
        var customToData: T? = null
        var isEnter: Boolean? = null


        fun updateByView(status:String, shareElementView: View?){
            if(clz != null){
                if(status == DATA_STATUS_CAPTURE_SNAPSHOT){
                    customTempData = clz?.newInstance()
                    customTempData?.update(shareElementView)
                } else if(this.status == DATA_STATUS_CAPTURE_SNAPSHOT && status == DATA_STATUS_START){ //进入新界面
                    customFromData = customTempData
                    isEnter = true
                }else if(this.status == DATA_STATUS_START && status == DATA_STATUS_END){
                    customToData = clz?.newInstance()
                    customToData?.update(shareElementView)
                } else if(this.status == DATA_STATUS_CAPTURE_SNAPSHOT && status == DATA_STATUS_END){ //返回 前一个界面
                    customToData = customTempData
                    isEnter = false
                }else if(this.status == DATA_STATUS_END && status == DATA_STATUS_START){
                    customFromData = clz?.newInstance()
                    customFromData?.update(shareElementView)
                }
            }
            this.status = status
        }

        constructor(parcel: Parcel) : this() {
            this.snapShot = parcel.readParcelable(Parcelable::class.java.classLoader)
            this.customFromData = parcel.readParcelable(Parcelable::class.java.classLoader)
            this.customToData = parcel.readParcelable(Parcelable::class.java.classLoader)
        }

        constructor(snapShot: Parcelable, clz : Class<T>?) : this() {
            this.snapShot = snapShot
            this.clz = clz
        }

        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeParcelable(snapShot, flags)
            parcel.writeParcelable(customFromData, flags)
            parcel.writeParcelable(customToData, flags)
        }

        override fun describeContents(): Int {
            return 0
        }

        companion object {
            const val DATA_STATUS_CAPTURE_SNAPSHOT = "onCaptureSharedElementSnapshot"
            const val DATA_STATUS_START = "onSharedElementStart"
            const val DATA_STATUS_END = "onSharedElementEnd"

            fun getFromView(view: View?): ShareElementInfoData<*>? {
                if (view == null) {
                    return null
                }
                val tag = view.getTag(R.id.share_element_info_data)
                return if (tag is ShareElementInfoData<*>) tag else null
            }

            fun saveToView(view: View?, info: ShareElementInfoData<*>?) {
                if (view == null) {
                    return
                }
                view.setTag(R.id.share_element_info_data, info)
            }

            @JvmField val CREATOR: Parcelable.Creator<ShareElementInfoData<ShareElementInfoDataUpdate>> = object : Parcelable.Creator<ShareElementInfoData<ShareElementInfoDataUpdate>> {
                override fun createFromParcel(parcel: Parcel): ShareElementInfoData<ShareElementInfoDataUpdate> {
                    return ShareElementInfoData(parcel)
                }

                override fun newArray(size: Int): Array<ShareElementInfoData<ShareElementInfoDataUpdate>> {
                    return newArray(size)
                }
            }
        }
    }

}